/**
 *****************************************************************************
 * DESC ：. 
 * Author : Junwei Zhang 
 * CREATEDATE ：2012-8-17 
 * MODIFYLIST ：       	Name 			 Date 			Reason/Contents
 * 				------------------------------------------------------ 
 * 				 	Junwei Zhang     2012-8-17		Created.
 ****************************************************************************
 **/
package com.me.server;

import java.util.Calendar;
import java.util.Date;

/**
 * 
 * <br/>
 * 下午8:33:04 2012-8-17
 * 
 * @Author <a href="mailto:mestudying@sina.com">Junwei Zhang</a><br/>
 **/
public class DateMan {

	private long unixTime = 0;
	private Date myDate;
	private String yearS = ("0000");
	private String monthS = ("00");
	private String dayS = ("00");
	private String hourS = ("00");
	private String minS = ("00");
	private String secS = ("00");

	private int day = 0, month = 0, year = 0;
	private int sec = 0, min = 0, hour = 0;

	public DateMan() {
		this.setTime(-1);
	}

	public DateMan(long unixTime) {
		this.setTime(unixTime);
	}

	public void setTime(long unixTime) {
		this.unixTime = unixTime * 1000;

		if (this.unixTime >= 0) {
			myDate = new Date(this.unixTime);
			Calendar cal = Calendar.getInstance();
			cal.setTime(myDate);
			this.year = cal.get(Calendar.YEAR);
			this.month = cal.get(Calendar.MONTH) + 1;
			this.day = cal.get(Calendar.DAY_OF_MONTH);
			this.hour = cal.get(Calendar.HOUR_OF_DAY);
			this.min = cal.get(Calendar.MINUTE);
			this.sec = cal.get(Calendar.SECOND);

			yearS = Integer.toString(this.year);

			if (this.month >= 10)
				monthS = Integer.toString(this.month);
			else
				monthS = "0" + Integer.toString(this.month);

			if (this.day >= 10)
				dayS = Integer.toString(this.day);
			else
				dayS = "0" + Integer.toString(this.day);

			if (this.hour >= 10)
				hourS = Integer.toString(this.hour);
			else
				hourS = "0" + Integer.toString(this.hour);

			if (this.min >= 10)
				minS = Integer.toString(this.min);
			else
				minS = "0" + Integer.toString(this.min);

			if (this.sec >= 10)
				secS = Integer.toString(this.sec);
			else
				secS = "0" + Integer.toString(this.sec);
		} else {
			myDate = new Date();
			Calendar cal = Calendar.getInstance();
			cal.setTime(myDate);
			this.unixTime = cal.getTimeInMillis();
			this.setTime(this.unixTime / 1000);
		}
	}

	public String getTime(int style) {
		if (this.unixTime < 0) {
			return "undefined";
		}

		if (style == 0)
			return (yearS + "-" + monthS + "-" + dayS + " " + hourS + ":" + minS + ":" + secS);

		if (style == 1)
			return (yearS + "-" + monthS + "-" + dayS);

		if (style == 2)
			return (hourS + ":" + minS + ":" + secS);

		if (style == 3)
			return (hourS + ":" + minS);

		if (style == 4)
			return (minS + ":" + secS);

		if (style == 5)
			return (monthS + "-" + dayS + " " + hourS + ":" + minS);

		return null;
	}

}
